import Mock from 'mockjs2'
import { builder, getBody, getQueryParameters } from '../winter-util'

const add = (options) => {
  const body = getBody(options)
  console.log('mock: body', body)
  return builder({ ...body, 'id': Mock.mock('@guid') }, '', 200)
}

const del = (options) => {
  const body = getBody(options)
  console.log('mock: body', body)
  return builder({ ...body, 'id': Mock.mock('@guid') }, '', 200)
}

const page = (options) => {
  const queryParameters = getQueryParameters(options)
  console.log('queryParameters', queryParameters)
  if (queryParameters && !queryParameters.size) {
    queryParameters.size = 10
  }
  const data = []
  for (let i = 0; i < queryParameters.size; i++) {
    const tmpKey = i + 1
    data.push({
      id: tmpKey,
      name: Mock.mock('@name'),
      code: Mock.mock('@guid'),
      createTime: '2019-01-01 10:22',
      editTime: '2019-01-01 10:22'
    })
  }
  const res = builder({
    size: parseInt(queryParameters.size),
    page: parseInt(queryParameters.page),
    list: data,
    total: 25
  }, '', 200)
  return res
}

const edit = (options) => {
  const body = getBody(options)
  console.log('mock: body', body)
  return builder({ ...body }, '', 200)
}

Mock.mock(/\/demo\/crud/, 'post', add)
Mock.mock(/\/demo\/crud\/1/, 'delete', del)
Mock.mock(/\/demo\/crud\/page/, 'get', page)
Mock.mock(/\/demo\/crud/, 'put', edit)
